/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cuong.bean;

import client.entity.Category;
import client.entity.Customer;
import client.entity.JobSubject;
import client.entity.LevelJob;
import client.entity.Location;
import client.entity.Recruitment;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.HibernateUtil;

/**
 *
 * @author Admin Windows 8
 */
@ManagedBean
@SessionScoped
public class ComboboxSearchBean {

    private String status = "fail";
    private int category;
    private int loveljob;
    private int location;
    private List<Category> categorys;
    private List<LevelJob> loveljobs;
    private List<Location> locations;
    private List<JobSubject> list;
    private Map<String, String> params;
    private List<Recruitment> listRecruiment;
    
    private static final int DEFAULT_RECORDS_NUMBER = 10;
    private int count = 0;
    private int trang = 1;
    private int tongtrang;

    public ComboboxSearchBean() {
    }   

    public void next() {
        if (trang < tongtrang) {
            trang++;
            count++;
        }
    }

    public void prev() {
        if (this.trang > 1) {
            count--;
            trang--;
        }
    }

    public void first() {
        trang = 1;
        count = 0;

    }

    public void last() {
        trang = tongtrang;
        count = trang - 1;
    }

    public int getTrang() {
        return trang;
    }

    public void setTrang(int trang) {
        this.trang = trang;
    }

    public int getTongtrang() {
        int totalRc = getSizeDataByID();
        tongtrang = totalRc / DEFAULT_RECORDS_NUMBER;
        if (totalRc % DEFAULT_RECORDS_NUMBER > 0) {
            tongtrang++;
        }
        return tongtrang;
    }

    public void setTongtrang(int tongtrang) {
        this.tongtrang = tongtrang;
    }

    public List<Category> getCategorys() {
        categorys = new ArrayList<Category>();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        categorys = ss.createQuery("from Category").list();
        ss.getTransaction().commit();
        ss.close();
        return categorys;
    }

    public void setCategorys(List<Category> categorys) {
        this.categorys = categorys;
    }

    public List<LevelJob> getLoveljobs() {
        loveljobs = new ArrayList<LevelJob>();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        loveljobs = ss.createQuery("from LevelJob").list();
        ss.getTransaction().commit();
        ss.close();
        return loveljobs;
    }

    public void setLoveljobs(List<LevelJob> loveljobs) {
        this.loveljobs = loveljobs;
    }

    public List<Location> getLocations() {
        locations = new ArrayList<Location>();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        locations = ss.createQuery("from Location").list();
        ss.getTransaction().commit();
        ss.close();
        return locations;
    }

    public void setLocations(List<Location> locations) {
        this.locations = locations;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public int getCategory() {
        return category;
    }

    public void setCategory(int category) {
        this.category = category;
    }

    public int getLoveljob() {
        return loveljob;
    }

    public void setLoveljob(int loveljob) {
        this.loveljob = loveljob;
    }

    public int getLocation() {
        return location;
    }

    public void setLocation(int location) {
        this.location = location;
    }

    public List<JobSubject> getList() {
        list = new ArrayList<JobSubject>();
        List<Recruitment> listRe = getListRecruiment();
        for (Recruitment recruitment : listRe) {
            JobSubject js = new JobSubject();
            js.setId(recruitment.getId());
            js.setTitle(recruitment.getTitle());
            if (recruitment.getTypeOfJob() == true) {
                js.setTypejob("Full Time");
            } else {
                js.setTypejob("Pass Time");
            }
            js.setNamecustomer(getCustomerByID(recruitment.getCustomer().getId()).getName());
            js.setNameleveljob(getLevelJobByID(recruitment.getLevelJob().getId()).getName());

            List<Category> listCate = getListCateByIDRecrui(recruitment.getId());
            StringBuffer stringCate = new StringBuffer();
            for (int i = 0; i < listCate.size(); i++) {
                Category ct = listCate.get(i);
                if (i < listCate.size() - 1) {
                    stringCate.append(ct.getCategoryName() + ", ");
                } else {
                    stringCate.append(ct.getCategoryName());
                }
            }
            js.setNamecate(stringCate.toString());

            List<Location> listLoca = getListLocaByIDRecrui(recruitment.getId());
            StringBuffer stringLocale = new StringBuffer();
            for (int i = 0; i < listLoca.size(); i++) {
                Location lc = listLoca.get(i);
                if (i < listLoca.size() - 1) {
                    stringLocale.append(lc.getName() + ", ");
                } else {
                    stringLocale.append(lc.getName());
                }
            }
            js.setNamelocation(stringLocale.toString());
            list.add(js);
        }
        return list;
    }  

    public List<Recruitment> getListRecruiment() {
        String prIDct = getParam("idct");
        if (prIDct == null) {
            category = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("cateid");
        } else {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("cateid", category);
        }
        String prIDlj = getParam("idlj");
        if (prIDlj == null) {
            loveljob = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("leveljobid");
        } else {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("leveljobid", loveljob);
        }
        String prIDlc = getParam("idlc");
        if (prIDlc == null) {
            location = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("localid");
        } else {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("localid", location);
        }

        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        if (category == -1) {
            if (location == -1) {
                if (loveljob == -1) {
                    listRecruiment = ss.createQuery("from Recruitment").setFirstResult(count * DEFAULT_RECORDS_NUMBER).setMaxResults(DEFAULT_RECORDS_NUMBER).list();
                } else {
                    listRecruiment = ss.createQuery("select re from Recruitment as re where re.levelJob.id=" + loveljob)
                            .setFirstResult(count * DEFAULT_RECORDS_NUMBER).setMaxResults(DEFAULT_RECORDS_NUMBER).list();
                }
            } else {
                if (loveljob == -1) {
                    listRecruiment = ss.createQuery("select re from Recruitment as re inner join re.recruitmentLocations as rl where rl.location.id=" + location)
                            .setFirstResult(count * DEFAULT_RECORDS_NUMBER).setMaxResults(DEFAULT_RECORDS_NUMBER).list();

                } else {
                    listRecruiment = ss.createQuery("select re from Recruitment as re inner join re.recruitmentLocations as l  where l.location.id=" + location + " and re.levelJob.id=" + loveljob)
                            .setFirstResult(count * DEFAULT_RECORDS_NUMBER).setMaxResults(DEFAULT_RECORDS_NUMBER).list();
                }
            }
        } else {
            if (location == -1) {
                if (loveljob == -1) {
                    listRecruiment = ss.createQuery("select re from Recruitment as re inner join re.recruitmentCategories as rc where rc.category.id=" + category)
                            .setFirstResult(count * DEFAULT_RECORDS_NUMBER).setMaxResults(DEFAULT_RECORDS_NUMBER).list();
                } else {
                    listRecruiment = ss.createQuery("select re from Recruitment as re inner join re.recruitmentCategories as c  where c.category.id=" + category + " and re.levelJob.id=" + loveljob)
                            .setFirstResult(count * DEFAULT_RECORDS_NUMBER).setMaxResults(DEFAULT_RECORDS_NUMBER).list();
                }
            } else {
                if (loveljob == -1) {
                    listRecruiment = ss.createQuery("select re from Recruitment as re inner join re.recruitmentCategories as c inner join re.recruitmentLocations as l  where c.category.id=" + category + " and l.location.id=" + location)
                            .setFirstResult(count * DEFAULT_RECORDS_NUMBER).setMaxResults(DEFAULT_RECORDS_NUMBER).list();
                } else {
                    listRecruiment = ss.createQuery("select re from Recruitment as re inner join re.recruitmentCategories as c inner join re.recruitmentLocations as l  where c.category.id=" + category + " and l.location.id=" + location + " and re.levelJob.id=" + loveljob)
                            .setFirstResult(count * DEFAULT_RECORDS_NUMBER).setMaxResults(DEFAULT_RECORDS_NUMBER).list();
                }
            }
        }

        ss.getTransaction().commit();
        ss.close();
        return listRecruiment;
    }

    public List<Category> getListCateByIDRecrui(int idRecrui) {
        List<Category> list = new ArrayList<Category>();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        list = ss.createQuery("select c from Category as c inner join c.recruitmentCategories as rc where rc.recruitment.id=" + idRecrui).list();
        ss.getTransaction().commit();
        ss.close();
        return list;
    }

    public List<Location> getListLocaByIDRecrui(int idRecrui) {
        List<Location> list = new ArrayList<Location>();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        list = ss.createQuery("select lo from Location as lo inner join lo.recruitmentLocations as rl where rl.recruitment.id=" + idRecrui).list();
        ss.getTransaction().commit();
        ss.close();
        return list;
    }

    public LevelJob getLevelJobByID(int id) {
        LevelJob lj = new LevelJob();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        lj = (LevelJob) ss.createQuery("from LevelJob lj where lj.id=" + id).uniqueResult();
        ss.getTransaction().commit();
        ss.close();
        return lj;
    }

    public Customer getCustomerByID(int id) {
        Customer lj = new Customer();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        lj = (Customer) ss.createQuery("from Customer cus where cus.id=" + id).uniqueResult();
        ss.getTransaction().commit();
        ss.close();
        return lj;
    }

    public Map<String, String> getParams() {
        return params;
    }

    public void setParams(Map<String, String> params) {
        this.params = params;
    }

    public String getParam(String param) {
        params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        return params.get(param);
    }

    public void searchComboboxListener() {
        if (status.equals("fail")) {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please Choice Value Required ", "Please Try Again");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }
     public int getSizeDataByID() {//5
         int count;
         String prIDct = getParam("idct");
        if (prIDct == null) {
            category = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("cateid");
        } else {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("cateid", category);
        }
        String prIDlj = getParam("idlj");
        if (prIDlj == null) {
            loveljob = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("leveljobid");
        } else {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("leveljobid", loveljob);
        }
        String prIDlc = getParam("idlc");
        if (prIDlc == null) {
            location = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("localid");
        } else {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("localid", location);
        }

        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        if (category == -1) {
            if (location == -1) {
                if (loveljob == -1) {
                    count = ((Long)ss.createQuery("select count(*) from Recruitment").iterate().next()).intValue();
                } else {
                    count = ((Long)ss.createQuery("select count(re) from Recruitment as re where re.levelJob.id=" + loveljob).iterate().next()).intValue();                    
                }
            } else {
                if (loveljob == -1) {
                    count = ((Long)ss.createQuery("select count(re) from Recruitment as re inner join re.recruitmentLocations as rl where rl.location.id=" + location).iterate().next()).intValue();  
                } else {
                    count = ((Long)ss.createQuery("select count(re) from Recruitment as re inner join re.recruitmentLocations as l  where l.location.id=" + location + " and re.levelJob.id=" + loveljob).iterate().next()).intValue();  
                }
            }
        } else {
            if (location == -1) {
                if (loveljob == -1) {
                    count = ((Long)ss.createQuery("select count(re)  from Recruitment as re inner join re.recruitmentCategories as rc where rc.category.id=" + category).iterate().next()).intValue();  
                } else {
                    count = ((Long)ss.createQuery("select count(re) from Recruitment as re inner join re.recruitmentCategories as c  where c.category.id=" + category + " and re.levelJob.id=" + loveljob).iterate().next()).intValue();  
                   
                }
            } else {
                if (loveljob == -1) {
                    count = ((Long)ss.createQuery("select count(re) from Recruitment as re inner join re.recruitmentCategories as c inner join re.recruitmentLocations as l  where c.category.id=" + category + " and l.location.id=" + location).iterate().next()).intValue();  
                    
                } else {
                     count = ((Long)ss.createQuery("select count(re) from Recruitment as re inner join re.recruitmentCategories as c inner join re.recruitmentLocations as l  where c.category.id=" + category + " and l.location.id=" + location + " and re.levelJob.id=" + loveljob).iterate().next()).intValue();
                }
            }
        }

        ss.getTransaction().commit();
        ss.close();
        return count;
    }
}
